from flask import Blueprint,request,jsonify,make_response
from  flask import  current_app as app
import json
import os
import uuid
from utils.mylist import myfile
from utils.db import  db
from utils.comm import xtree
from utils.myes import Es
from utils.comm import  get_data
from utils.jwts import myjwt
from utils.myacl import acl
from utils.redisconn import  r

bp_user = Blueprint('users',__name__)


#用户登录
@bp_user.route('/login/',methods=['POST'])
def login():
    data = get_data()
    sql = "select * from users where mobile='%s'"%(data['mobile'])
    res = db.find_one(sql)
    #判断用户是否存在
    if res:
        if res['password'] == data['password']:
            #生成token
            data = {'data':{'mobile':res['mobile'],'userid':res['id'],'name':res['name']}}
            token = myjwt.jwt_encode(data)
            #用户权限 该用户权限集 这边只获取前端权限
            resall = acl.get_page_resource(res['id'])
            #把前端所有权限存入redis
            key = 'interface'+str(res['id'])
            r.set_add(key,resall)
            return jsonify({"code":200,'mes':'登录成功','userid':res['id'],'name':res['name'],'mobile':res['mobile'],'token':token,'pagelist':resall})
        else:
            return jsonify({"code":20030,'mes':'用户名或密码有误'})
    else:
        return jsonify({"code":20020,'mes':'该用户不从在'})





